package org.esti.controller;

import org.esti.model.User;
import org.esti.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author ALoginov <a href="mailto:Artem.Loginov@exigenservices.com">Artem Loginov</a>
 * @since 10/18/11
 */
@Controller
public class UserController {
    private static final String USERS_VIEW_NAME = "users";
    private static final String USERS_MODEL_ATTRIBUTE_NAME = "users";

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String showUsers(Model model) {
        List<User> users = userService.getUsers();
        model.addAttribute(USERS_MODEL_ATTRIBUTE_NAME, users);
        return USERS_VIEW_NAME;
    }

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public String showFilteredUsers(
            @RequestParam String keyword,
            Model model) {
        List<User> users = userService.getUsers(keyword);
        model.addAttribute(USERS_MODEL_ATTRIBUTE_NAME, users);
        return USERS_VIEW_NAME;
    }
}